package com.lcc.lynx.base.system.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.lcc.lynx.base.system.mapper.SysUserMapper;
import com.lcc.lynx.base.system.pojo.dv.SysUserDV;
import com.lcc.lynx.base.system.pojo.dv.SysUserRoleDV;
import com.lcc.lynx.base.system.service.SysUserRoleService;
import com.lcc.lynx.base.system.service.SysUserService;
import com.lcc.lynx.common.utils.StrUtil;
import com.lcc.lynx.security.config.SecurityProperties;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 系统用户服务实现
 * @auth lcc
 * @date 2025-01-01
 */
@Service
@Transactional
@AllArgsConstructor
public class SysUserServiceImpl implements SysUserService {

    private SysUserMapper mapper;
    private SysUserRoleService userRoleService;
    private SecurityProperties securityProperties;


    @Override
    public SysUserMapper getMapper() {
       return mapper;
    }

    @Override
    public void addAndRole(SysUserDV dv) {
        if(dv.getPassword() == null){
            // 123456
            dv.setPassword(SecureUtil.hmacMd5(securityProperties.getMd5Key()).digestHex(securityProperties.getDefaultPassword()));
        }
        this.insert(dv);
        SysUserRoleDV publicRole = new SysUserRoleDV();
        publicRole.setRoleId(24L);
        if(dv.getRoleList() == null) {
            dv.setRoleList(new ArrayList<>());
        }
        dv.getRoleList().add(publicRole);
        for (SysUserRoleDV RoleDV : dv.getRoleList()) {
            RoleDV.setUserId(dv.getUserId());
        }
        userRoleService.insertList(dv.getRoleList());
    }

    @Override
    public void updateAndRole(SysUserDV dv) {
        this.updateById(dv);
        if(dv.getRoleList() != null) {
            SysUserRoleDV RoleDVParams = new SysUserRoleDV();
            RoleDVParams.setUserId(dv.getUserId());
            List<SysUserRoleDV> oldList = userRoleService.select(RoleDVParams);
            // 查询出删除的
            List<Long> delIdList = oldList.stream()
                    .filter(old -> dv.getRoleList().stream().noneMatch(newRole -> old.getRoleId().equals(newRole.getRoleId())))
                    .map(SysUserRoleDV::getId).toList();
            if(delIdList.size() > 0) {
                userRoleService.deleteByIdList(delIdList);
            }

            // 查出新增的
            List<SysUserRoleDV> addList = dv.getRoleList().stream()
                    .filter(newRole -> oldList.stream().noneMatch(old -> old.getRoleId().equals(newRole.getRoleId()))).toList();
            for (SysUserRoleDV RoleDV : addList) {
                RoleDV.setUserId(dv.getUserId());
            }
            userRoleService.insertList(addList);
        }
    }

    @Override
    public List<String> queryUserPermissions(SysUserDV userInfo) {
        Set<String> permissions = new HashSet<>();
        List<String> strings = mapper.queryUserPermissions(userInfo);
        for (String string : strings) {
            if(StrUtil.isEmpty(string)){
                continue;
            }
            if(string.contains(",")) {
                String[] split = string.split(",");
                for (String s : split) {
                    permissions.add(s);
                }
            }else{
                permissions.add(string);
            }
        }
        return permissions.stream().toList();
    }
}